package Gui;

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import Objects.Body;
import Objects.BodyCircle;
import Objects.BodyRectangle;

@SuppressWarnings({ "serial"})
public class Toolbar extends JPanel implements ActionListener {

	
	@SuppressWarnings("unchecked")
	public HashMap []hM;
	public HashMap<Integer, Body> bodies;
    public HashMap<Body, BodyRectangle> btr;
    public HashMap<Body, BodyCircle> btc;
    BodyCircle circle;
    BodyRectangle rectangle;
	JToolBar tb;
	JButton addCir;
	JButton addRec;
	JButton start;
	public float x, y;
	int a,b,c;
	float d,e,f,g,h,i,j,k,l,m;
	int numcir, numrec;
	Boolean exit;
	JFrame frame;
	JFormattedTextField Cmass, Cradius, Cvx, Cvy, Cax, Cay, Cangle, Comega, Cfx, Cfy, Cepsilon;
	JFormattedTextField Rmass, Rvx, Rvy, Rax, Ray, Rangle, Romega, Rfx, Rfy, Repsilon, Rwidth, Rheight;
	JFrame setCircle, setRectangle;
	JButton Csubmit, Rsubmit;
	JPanel setC, setR;
	boolean ok=false;

	@SuppressWarnings("unchecked")
	public Toolbar(float x, float y, boolean exit, HashMap[] hM, JFrame frame) {
		this.x=x;
		this.y=y;
		numcir=0;
		numrec=0;
		this.exit=exit;
	    this.hM=hM;
	    bodies=hM[0];
	    btr=hM[1];
	    btc=hM[2];
	    this.frame=frame;
	}
	
	public void set(float x, float y){
		this.x=x;
		this.y=y;
	}
	
	public void init(){
		tb = new JToolBar ();
		//tb.setLocation((int)x,(int)y);
		tb.setFloatable(false);
		addCir = new JButton ("Add circle");
		addRec = new JButton ("Add rectangle");
		start = new JButton ("Start simulation");
		tb.add(addCir);
		tb.add(addRec);
		tb.add(start);
		addCir.addActionListener(this);
		addRec.addActionListener(this);
		start.addActionListener(this);
		add(tb);
		
		setCircle = new JFrame ("Circle");
		setCircle.setSize(200, 600);
		setC=new JPanel();
		
		Cmass= new JFormattedTextField("1");
		Cmass.setVisible(true);
		setC.add(Cmass);
				
		Cradius = new JFormattedTextField("10");
		Cradius.setVisible(true);
		setC.add(Cradius);
				
		Cvx = new JFormattedTextField("0");
		Cvx.setVisible(true);
		setC.add(Cvx);
				
		Cvy = new JFormattedTextField("0");
		Cvy.setVisible(true);
		setC.add(Cvy);
		
//		Cax = new JFormattedTextField("0");
//		Cax.setVisible(true);
//		setC.add(Cax);
//		
//		Cay = new JFormattedTextField("0");
//		Cay.setVisible(true);
//		setC.add(Cay);
			
		Cangle = new JFormattedTextField("0");
		Cangle.setVisible(true);
		setC.add(Cangle);
		
		Comega = new JFormattedTextField("0");
		Comega.setVisible(true);
		setC.add(Comega);
		
		Cfx = new JFormattedTextField("0"); 
		Cfx.setVisible(true);
		setC.add(Cfx);
		
		Cfy = new JFormattedTextField("0");
		Cfy.setVisible(true);
		setC.add(Cfy);
		
		Cepsilon = new JFormattedTextField("0");
		Cepsilon.setVisible(true);
		setC.add(Cepsilon);
		
		Csubmit = new JButton("Submit");
		Csubmit.addActionListener(this);
		setC.add(Csubmit);
		
		Cmass.setBorder(BorderFactory.createTitledBorder("Mass"));
		Cradius.setBorder(BorderFactory.createTitledBorder("Radius"));
		Cvx.setBorder(BorderFactory.createTitledBorder("Velocity along X-axis"));
		Cvy.setBorder(BorderFactory.createTitledBorder("Velocity along Y-axis"));
		Cax.setBorder(BorderFactory.createTitledBorder("Acceleration along X-axis"));
		Cay.setBorder(BorderFactory.createTitledBorder("Acceleration along Y-axis"));
		Cangle.setBorder(BorderFactory.createTitledBorder("Angle"));
		Comega.setBorder(BorderFactory.createTitledBorder("Angular velocity"));
		Cfx.setBorder(BorderFactory.createTitledBorder("Force along X-axis"));
		Cfy.setBorder(BorderFactory.createTitledBorder("Force along Y-axis"));
		Cepsilon.setBorder(BorderFactory.createTitledBorder("Angular acceleration"));
		
		setRectangle = new JFrame ();
		setRectangle.setSize(200, 600);
		setR=new JPanel();
		
		Rmass= new JFormattedTextField("1");
		Rmass.setVisible(true);
		setR.add(Rmass);
		
		Rheight = new JFormattedTextField("10");
		Rheight.setVisible(true);
		setR.add(Rheight);
		
		Rwidth = new JFormattedTextField("10");
		Rwidth.setVisible(true);
		setR.add(Rwidth);
		
		Rvx = new JFormattedTextField("0");
		Rvx.setVisible(true);
		setR.add(Rvx);
		
		Rvy = new JFormattedTextField("0");
		Rvy.setVisible(true);
		setR.add(Rvy);
		
		Rax = new JFormattedTextField("0");
		Rax.setVisible(true);
		setR.add(Rax);
		
		Ray = new JFormattedTextField("0");
		Ray.setVisible(true);
		setR.add(Ray);
		
		Rangle = new JFormattedTextField("0");
		Rangle.setVisible(true);
		setR.add(Rangle);
		
		Romega = new JFormattedTextField("0");
		Romega.setVisible(true);
		setR.add(Romega);
		
		Rfx = new JFormattedTextField("0"); 
		Rfx.setVisible(true);
		setR.add(Rfx);
		
		Rfy = new JFormattedTextField("0");
		Rfy.setVisible(true);
		setR.add(Rfy);
		
		Repsilon = new JFormattedTextField("0");
		Repsilon.setVisible(true);
		setR.add(Repsilon);
		
		Rsubmit = new JButton("Submit");
		Rsubmit.addActionListener(this);
		setR.add(Rsubmit);
		
		Rmass.setBorder(BorderFactory.createTitledBorder("Mass"));
		Rvx.setBorder(BorderFactory.createTitledBorder("Velocity along X-axis"));
		Rvy.setBorder(BorderFactory.createTitledBorder("Velocity along Y-axis"));
		Rax.setBorder(BorderFactory.createTitledBorder("Acceleration along X-axis"));
		Ray.setBorder(BorderFactory.createTitledBorder("Acceleration along Y-axis"));
		Rangle.setBorder(BorderFactory.createTitledBorder("Angle"));
		Romega.setBorder(BorderFactory.createTitledBorder("Angular velocity"));
		Rfx.setBorder(BorderFactory.createTitledBorder("Force along X-axis"));
		Rfy.setBorder(BorderFactory.createTitledBorder("Force along Y-axis"));
		Repsilon.setBorder(BorderFactory.createTitledBorder("Angular acceleration"));
		Rwidth.setBorder(BorderFactory.createTitledBorder("Width"));
		Rheight.setBorder(BorderFactory.createTitledBorder("Height"));		
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		if(evt.getSource()==addCir){
			setC.setLayout((LayoutManager) new BoxLayout(setC, BoxLayout.PAGE_AXIS));
			setCircle.add(setC);
			setCircle.setVisible(true);
			setCircle.setResizable(false);
		}
		
		if(evt.getSource()==addRec){
			setRectangle.add(setR);
			setR.setLayout((LayoutManager) new BoxLayout(setR, BoxLayout.PAGE_AXIS));
			setRectangle.setVisible(true);
			setRectangle.setResizable(false);
			
		}
		if(evt.getSource()==start){
			exit=true;
			frame.setVisible(false);
		}
		if(evt.getSource()==Csubmit){
			ok=true;
			if(ok){
				String s;
				s=Cmass.getText();
				if(s!=null || Integer.parseInt(s)<=0){
					b = Integer.parseInt(s);
				}
				else{
					b = 1;
				}
				
				s=Cradius.getText();
				if(s!=null){
					c = Integer.parseInt(s);
				}
				else{
					c = 0;
				}
				
				s=Cvx.getText();
				if(s!=null){
					d = Float.parseFloat(s);
				}
				else{
					d = 0;
				}
				
				s=Cvy.getText();
				if(s!=null){
					e = Float.parseFloat(s);
				}
				else{
					e = 0;
				}
				
//				s=Cax.getText();
//				if(s!=null){
//					f = Float.parseFloat(s);
//				}
//				else{
//					f = 0;
//				}
//				
//				s=Cay.getText();
//				if(s!=null){
//					g = Float.parseFloat(s);
//				}
//				else{
//					g = 0;
//				}
				
				s=Cangle.getText();
				if(s!=null){
					h = Float.parseFloat(s);
				}
				else{
					h = 0;
				}
				
				s=Comega.getText();
				if(s!=null){
					i = Float.parseFloat(s);
				}
				else{
					i = 0;
				}
				
				s=Cfx.getText();
				if(s!=null){
					j = Float.parseFloat(s);
				}
				else{
					j = 0;
				}
				
				s=Cfy.getText();
				if(s!=null){
					k = Float.parseFloat(s);
				}
				else{
					k = 0;
				}
				
				s=Cepsilon.getText();
				if(s!=null){
					l = Float.parseFloat(s);
				}
				else{
					l = 0;
				}
				circle=new BodyCircle(b, x, y, c, d, e, h, i, j, k, l);
				bodies.put(bodies.size(),circle);
				btc.put(bodies.get(bodies.size()-1),circle);
				numcir++;
				frame.setVisible(false);
				setCircle.setVisible(false);
				ok=false;
			}
		}
		if(evt.getSource()==Rsubmit){
			ok=true;
			if(ok){
				String s;
				s=Rmass.getText();
				if(s!=null || Integer.parseInt(s)<=0){
					b = Integer.parseInt(s);
				}
				else{
					b = 1;
				}
				
				s=Rwidth.getText();
				if(s!=null){
					c = Integer.parseInt(s);
				}
				else{
					c = 0;
				}
				
				s=Rheight.getText();
				if(s!=null){
					a = Integer.parseInt(s);
				}
				else{
					a = 0;
				}
				
				s=Rvx.getText();
				if(s!=null){
					d = Float.parseFloat(s);
				}
				else{
					d = 0;
				}
				
				s=Rvy.getText();
				if(s!=null){
					e = Float.parseFloat(s);
				}
				else{
					e = 0;
				}
				
				s=Rax.getText();
				if(s!=null){
					f = Float.parseFloat(s);
				}
				else{
					f = 0;
				}
				
				s=Ray.getText();
				if(s!=null){
					g = Float.parseFloat(s);
				}
				else{
					g = 0;
				}
				
				s=Rangle.getText();
				if(s!=null){
					h = Float.parseFloat(s);
				}
				else{
					h = 0;
				}
				
				s=Romega.getText();
				if(s!=null){
					i = Float.parseFloat(s);
				}
				else{
					i = 0;
				}
				
				s=Rfx.getText();
				if(s!=null){
					j = Float.parseFloat(s);
				}
				else{
					j = 0;
				}
				
				s=Rfy.getText();
				if(s!=null){
					k = Float.parseFloat(s);
				}
				else{
					k = 0;
				}
				
				s=Repsilon.getText();
				if(s!=null){
					l = Float.parseFloat(s);
				}
				else{
					l = 0;
				}
			//	rectangle=new BodyRectangle(b, d, e, c, a, 0, 0, f, g, h, i, j, k, l);
				rectangle=new BodyRectangle(b, x, y, c, a, d, e, f, g, h, i, j, k, l);
				bodies.put(bodies.size(),rectangle);
				btr.put(bodies.get(bodies.size()-1),rectangle);
				numrec++;
				frame.setVisible(false);
				setRectangle.setVisible(false);
				ok=false;
			}

		}
	}
	
	
}
